var operation = {};
var postPath = '/gdp'

var app = new Vue({
    el: '#main-container',
    data: {
        // 应用id
        appId: '',
        // 操作类型
        optFlag: false,
        // 应用的分组信息
        catalogInfoList: []
    },
    computed: {},

    created: function () {

    },

    watch: {},
    mounted: function () {
        this.initData();

    },
    methods: {
        /**
         * 初始化数据
         */
        initData() {
            // 操作类型
            let opt = this.getQueryString("opt");
            // 主键
            let appId = this.getQueryString("appId");
            // 应用id初始化
            this.appId = appId;
            if (opt === 'editGroup') {
                this.optFlag = true;
                // 获取数据
                this.selectById(appId);
            }
        },


        /**
         * 打开分组弹窗
         * @param item
         */
        openGroup(item) {
            let catalogId = null;
            let catalogName = null;
            let remark = null;
            if (item) {
                catalogId = item.catalogId;
                catalogName = item.catalogName;
                remark = item.remark;
            }
            $("#groupInfoContent").modal("show");
            $("#myIframe").attr("src", postPath + "/applicationGroupInfo/index?catalogId=" + catalogId)
        },

        delGroup(item) {
            let _this = this;
            let catalogId = item.catalogId;
            // 保存数据
            $.ajax({
                type: 'POST',
                url: postPath + '/accesscontrol/acAppCatalog/delCatalogByIds',
                data: {"catalogIds": catalogId},
                async: false,
                success: function (res) {
                    if (res.success) {
                        $("#groupInfoContent").modal('hide');
                        _this.initData()
                        showMsg("操作成功!", "success");
                    } else {
                        showMsg(res.msg, "error");
                    }
                }
            });

        },

        /**
         * 返回按钮
         */
        backBtn() {
            window.location.href = postPath + "/applicationCode/index"
        },

        /**
         * 查询配置
         */
        selectById(appId) {
            let _this = this;
            $.ajax({
                type: 'GET',
                url: postPath + '/accesscontrol/acAppCatalog/selectList',
                data: {appId: appId},
                async: false,
                success: function (res) {
                    if (res.success) {
                        _this.catalogInfoList = res.data;
                    } else {
                        showMsg("操作失败,请稍后重试!", "error");
                    }
                }
            });
        },

        /**
         * 保存分组信息
         */
        saveGroupInfo() {
            let _this = this;
            // 获取弹窗中绑定的vue对象
            let groupInfoVue = document.querySelector("iframe").contentWindow.document.getElementById('main-container').__vue__;
            // 获取 vue实例中的数据
            let groupInfoVueData = groupInfoVue.$data;
            // 获取vue实例中的数据的分组信息
            let params = groupInfoVueData.catalogInfo;
            // 校验
            if (!params.catalogName) {
                groupInfoVue.showErrorMsg("请填写分组名称");
                return
            }
            let tmpList = []
            // 循环选中的集合 进行封装
            groupInfoVueData.checkedStationList.forEach(i => {
                tmpList.push({stcd: i.stcd});
            })
            // 应用id
            params.appId = this.appId;
            // // 选中的站点
            params.appStationList = tmpList;
            // 保存数据
            $.ajax({
                type: 'POST',
                url: postPath + '/accesscontrol/acAppCatalog/save',
                data: JSON.stringify(params),
                dataType: 'json',
                async: false,
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.success) {
                        $("#groupInfoContent").modal('hide');
                        _this.initData()
                        showMsg("操作成功!", "success");
                    } else {
                        showMsg("操作失败,请稍后重试!", "error");
                    }
                }
            });
            
            // 推送数据
            $.ajax({
                type: 'POST',
                url: postPath + '/accesscontrol/appserviceaccessconfig/pushStation',
                data: JSON.stringify(params),
                dataType: 'json',
                async: true,
                contentType: 'application/json;charset=UTF-8',
                success: function (res) {
                    if (res.success) {
                        console.log('推送成功')
                    } else {
                    }
                }
            });

        },

        //获取Query参数
        getQueryString: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        },
    }
})


/*提示框*/
function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}


function confirmDelete(message, confirmFunction) {
    var messageObj = {
        size:'small',
        title: '提示',
        message: message,
        callback: confirmFunction,
        buttons: {
            confirm: {
                label: "确定"
            },
            cancel: {
                label: "取消"
            }
        },
    }
    bootbox.confirm(messageObj);
}
